import { _decorator, Button } from 'cc';
import { EditBox } from 'cc';
import { BaseView } from '../../../../scripts/libs/rxjs/cc3/BaseView';
import { ReactiveProperty } from '../../../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { BindEditBox } from '../../../../scripts/libs/rxjs/cc3/BindEditBox';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { gm } from '../../../../scripts/managers/GameManager';
import { StageInfo } from '../../game/basic';
import { EditorCreatePanel } from './EditorCreatePanel';
import { lm } from '../../LobbyManager';
import { FlowStatus } from '../../Enums';
const { ccclass, property } = _decorator;

@ccclass('EditorTools1')
export class EditorTools1 extends BaseView {

    editBoxText = ReactiveProperty.Create('');

    @property(EditBox)
    editBox: EditBox = null;

    @property(Button)
    loadButton: Button = null;

    @property(Button)
    saveButton: Button = null;

    @property(Button)
    newStageButton: Button = null;

    @property(Button)
    playButton: Button = null;

    useObserves() {
        return [
            BindEditBox(this.editBox).subscribe(_ => this.editBoxText.value = this.editBox.string),
            BindButton(this.loadButton).subscribe(this.loadButtonClicked.bind(this)),
            BindButton(this.saveButton).subscribe(this.saveButtonClicked.bind(this)),
            BindButton(this.newStageButton).subscribe(this.newStageButtonClicked.bind(this)),
            BindButton(this.playButton).subscribe(this.playButtonClicked.bind(this)),
            gm.stageInfoChanged.subscribe(this.onStageInfoChanged.bind(this))
        ];
    }

    loadButtonClicked() {
        let stageId = Number(this.editBoxText.value);
        if (Number.isNaN(stageId)) {
            let stageInfo = JSON.parse(this.editBoxText.value);
            gm.loadStageCommand.execute(stageInfo);
        } else {
            gm.loadStageCommand.execute(stageId as any);
        }
    }

    saveButtonClicked() {
        this.log('saveButtonClicked');
        let stageId = Number(this.editBoxText.value);
        if (Number.isNaN(stageId)) {
            let stageInfo = JSON.parse(this.editBoxText.value);
            gm.saveStageCommand.execute(stageInfo.stageId);
        } else {
            gm.saveStageCommand.execute(stageId);
        }
    }

    newStageButtonClicked() {
        EditorCreatePanel.StartProcess().subscribe(_ => {
            this.log('result:', _);
            if (_.status == 'confirm') {
                let stageInfo = {
                    stageId: _.stageId,
                    width: _.width,
                    height: _.height,
                };
                gm.loadStageCommand.execute(stageInfo as StageInfo);
            }
        })
    }

    onStageInfoChanged(stageInfo: StageInfo) {
        this.editBox.string = JSON.stringify(stageInfo);
        this.editBoxText.value = this.editBox.string;
    }

    playButtonClicked() {
        this.saveButtonClicked();
        lm.flowStatus.value = FlowStatus.Game;
    }

}


